#include "cc.h"
#include "bsp_clk.h"
#include "bsp_led.h"
#include "bsp_delay.h"
#include "bsp_beep.h"
#include "bsp_gpio.h"
#include "bsp_key.h"
#include "bsp_int.h"
#include "bsp_exti.h"
#include "bsp_epittimer.h"
#include "bsp_keyfilter.h"
#include "bsp_delay.h"
#include "bsp_uart.h"
#include "stdio.h"
#include "bsp_lcd.h"
#include "bsp_lcdapi.h"
#include "bsp_rtc.h"
#include "bsp_ap3216c.h"
#include "bsp_i2c.h"
#include "bsp_spi.h"
#include "bsp_icm20608.h"


char *banner = 	"========================================================\r\n"
				"正点原子I.MX6ULL ALPHA/Mini开发板Linux驱动之ARM裸机开发\r\n" \
				"--Date: 	%s\r\n" \
				"--Author: 	ChenHaoxu, Dimon.chen, 11813202388@qq.com\r\n" \
				"========================================================\r\n";


/*
 * @description	: 使能I.MX6U的硬件NEON和FPU
 * @param 		: 无
 * @return 		: 无
 */
 void imx6ul_hardfpu_enable(void)
{
	uint32_t cpacr;
	uint32_t fpexc;

	/* 使能NEON和FPU */
	cpacr = __get_CPACR();
	cpacr = (cpacr & ~(CPACR_ASEDIS_Msk | CPACR_D32DIS_Msk))
		   |  (3UL << CPACR_cp10_Pos) | (3UL << CPACR_cp11_Pos);
	__set_CPACR(cpacr);
	fpexc = __get_FPEXC();
	fpexc |= 0x40000000UL;	
	__set_FPEXC(fpexc);
}

int main(void)
{

	static uint8_t led_state = OFF;
	// static uint8_t beep_state = OFF;

	imx6ul_hardfpu_enable();	/* 使能I.MX6U的硬件浮点 			*/
	int_init();			/* 中断初始化 */
	imx6u_clkinit();	/* 时钟主频初始化，PLL1, PLL2, PLL3 */
	clk_init();			/* 使能所有外设时钟 */
	led_init();			/* led gpio 初始化 */
	beep_init();		/* beep gpio 初始化 */
	//key_init();			/* key gpio 初始化 */
	exti_init();		/* gpio外设中断初始化 */

	//epittimer_init(0, 0, 33000000);	/* EPIT分频frac=0 1分频，EPIT1时钟源66MHz，EPIT1->LR加载值计数器=33MHz，定时周期为500ms */
	//epittimer_init(1, 0, 66000000/20);	/* EPIT分频frac=0 1分频，EPIT1时钟源66MHz，EPIT1->LR加载值计数器=33MHz，定时周期为1000/20=50ms */

	keyfilter_init();
	delay_init();
	uart_init();		/* UART初始化 */
	//lcd_init();			/* LCD 初始化 */
	


	led_switch(LED_0, ON);


	beep_switch(ON);
	delay(200);
	beep_switch(OFF);
	delay(200);
	beep_switch(ON);
	delay(200);
	beep_switch(OFF);

	printf(banner, __DATE__);
	printf("Hello World\r\n");

	lcd_init();			/* LCD 初始化 */
	rtc_init();		
	

	i2c_ap3216c_init();
	icm20608_init();

	// unsigned short ir, ps, als;
	struct icm20608_device icm20608_dev;

	//启用I.MX6U浮点数计算


    while(1){
		led_state = !led_state;
		led_switch(LED_0, led_state);
		delay_ms(1000);

		// ap3216c_readdata(&ir, &ps, &als);
		// printf("ir=%d\r\n", ir);
		// printf("ps=%d\r\n", ps);
		// printf("als=%d\r\n", als);


		icm20608_read_dev_accel_gyro(&icm20608_dev);
		printf("accel %04x x:%hd\r\n", icm20608_dev.accel_x_adc, icm20608_dev.accel_x_adc);
		printf("accel %04x y:%hd\r\n", icm20608_dev.accel_y_adc, icm20608_dev.accel_y_adc);
		printf("accel %04x z:%hd\r\n", icm20608_dev.accel_z_adc, icm20608_dev.accel_z_adc);

		printf("gyro  %04x x:%hd\r\n", icm20608_dev.gyro_x_adc, icm20608_dev.gyro_x_adc);
		printf("gyro  %04x y:%hd\r\n", icm20608_dev.gyro_y_adc, icm20608_dev.gyro_y_adc);
		printf("gyro  %04x z:%hd\r\n", icm20608_dev.gyro_z_adc, icm20608_dev.gyro_z_adc);



		printf("ACT accel %04x x:%hd\r\n", icm20608_dev.accel_x_act, icm20608_dev.accel_x_act);
		printf("ACT accel %04x y:%hd\r\n", icm20608_dev.accel_y_act, icm20608_dev.accel_y_act);
		printf("ACT accel %04x z:%hd\r\n", icm20608_dev.accel_z_act, icm20608_dev.accel_z_act);

		printf("ACT gyro  %04x x:%hd\r\n", icm20608_dev.gyro_x_act, icm20608_dev.gyro_x_act);
		printf("ACT gyro  %04x y:%hd\r\n", icm20608_dev.gyro_y_act, icm20608_dev.gyro_y_act);
		printf("ACT gyro  %04x z:%hd\r\n", icm20608_dev.gyro_z_act, icm20608_dev.gyro_z_act);
    }

    return 0;
}